/*
* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#ifndef B2_SETTINGS_H
#define B2_SETTINGS_H

#include <assert.h>
#include <math.h>

#define B2_NOT_USED(x) x
#define b2Assert(A) assert(A)

//need to include NDS jtypes.h instead of 
//usual typedefs because NDS jtypes.h defines them slightly differently.
#ifdef TARGET_IS_NDS

#include "jtypes.h"

#else

typedef signed char int8;
typedef signed short int16;
typedef signed int int32;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;

#endif

#ifdef TARGET_FLOAT32_IS_FIXED

#include "Fixed.h"

typedef Fixed float32;
#define	B2_FLT_MAX	FIXED_MAX
#define	B2_FLT_EPSILON	FIXED_EPSILON
#define	B2FORCE_SCALE(x)	((x)<<7)
#define	B2FORCE_INV_SCALE(x)	((x)>>7)

#else

typedef float float32;
#define	B2_FLT_MAX	FLT_MAX
#define	B2_FLT_EPSILON	FLT_EPSILON
#define	B2FORCE_SCALE(x)	(x)
#define	B2FORCE_INV_SCALE(x)	(x)

#endif

#define b2_pi 3.14159265359f

/// @file
/// Global tuning contacts based on meters-kilograms-seconds(MKS) units.

// Collision.

/// The max number of contact points between two convex shapes.
#define b2_maxManifoldPoints	2

/// The maximum number of vertices on a convex polygon.
#define b2_maxPolygonVertices		8

/// Factor used to fatten AABBs in b2DynamicTree. This allows client
/// objects to move a small amount without needing to adjust the tree.
#define b2_fatAABBFactor			1.5f

/// The initial pool size for the dynamic tree.
#define b2_nodePoolSize				50

/// This must be a power of two
#define b2_maxProxies				512

/// This must be a power of two
#define b2_maxPairs					(8 * b2_maxProxies)

/// A small length used as a collision and constraint tolerance. Usually it is
/// chosen to be numerically significant, but visually insignificant.
#define b2_linearSlop				0.005f

/// A small angle used as a collision and constraint tolerance. Usually it is
/// chosen to be numerically significant, but visually insignificant.
#define b2_angularSlop				(2.0f / 180.0f * b2_pi)

/// The radius of the polygon/edge shape skin. This should not be modified. Making
/// this smaller means polygons will have and insufficient for continuous collision.
/// Making it larger may create artifacts for vertex collision.
#define b2_polygonRadius			(2.0f * b2_linearSlop)

// Dynamics

/// Maximum number of contacts to be handled to solve a TOI island.
#define b2_maxTOIContactsPerIsland	32

/// Maximum number of joints to be handled to solve a TOI island.
#define b2_maxTOIJointsPerIsland	32

/// A velocity threshold for elastic collisions. Any collision with a relative linear
/// velocity below this threshold will be treated as inelastic.
#define b2_velocityThreshold		1.0f

/// The maximum linear position correction used when solving constraints. This helps to
/// prevent overshoot.
#define b2_maxLinearCorrection		0.2f

/// The maximum angular position correction used when solving constraints. This helps to
/// prevent overshoot.
#define b2_maxAngularCorrection		(8.0f / 180.0f * b2_pi)

/// The maximum linear velocity of a body. This limit is very large and is used
/// to prevent numerical problems. You shouldn't need to adjust this.
#define b2_maxTranslation			2.0f
#define b2_maxTranslationSquared	(b2_maxTranslation * b2_maxTranslation)

/// The maximum angular velocity of a body. This limit is very large and is used
/// to prevent numerical problems. You shouldn't need to adjust this.
#define b2_maxRotation				(0.5f * b2_pi)
#define b2_maxRotationSquared		(b2_maxRotation * b2_maxRotation)

/// This scale factor controls how fast overlap is resolved. Ideally this would be 1 so
/// that overlap is removed in one time step. However using values close to 1 often lead
/// to overshoot.
#define b2_contactBaumgarte			0.2f

// Sleep

/// The time that a body must be still before it will go to sleep.
#define b2_timeToSleep				0.5f

/// A body cannot sleep if its linear velocity is above this tolerance.
#define b2_linearSleepTolerance		0.01f

/// A body cannot sleep if its angular velocity is above this tolerance.
#define b2_angularSleepTolerance	(2.0f / 180.0f * b2_pi)

//Memory Allocation

//The current number of bytes allocated by b2Alloc.
extern int32 b2_byteCount;

/// Implement this function to use your memory allocator.
void* b2Alloc(int32 size);

/// If you implement b2Alloc, you should implement this function too.
void b2Free(void* mem);

/// version number scheme.
struct b2Version
{
	int32 major;	/// significant changes.
	int32 minor;	/// incremental changes.
	int32 reversion;	/// bug fixs.
};

/// Current version.
extern b2Version b2_version;

/// Friction mixing law, feel free to customize this.
inline float32 b2MixFriction(float32 friction1, float32 friction2)
{
	return sqrtf(friction1*friction2);
}

/// Restitution law,Fell free to customize this.
inline float32 b2MixRestitution(float32 restitution1, float32 restitution2)
{
	return restitution1 > restitution2 ? restitution1 : restitution2;
}

#endif
